var express = require('express');
var router = express.Router();

const connection = require('./conn')

connection.connect(() => {
    console.log('数据库连接成功')
});


router.post('/updateUserById',(req,res)=>{
    let result = {};
    console.log(req.body);
    let {realname,age,idType,id} = req.body;
    let updateSql = 'update users set realname = ?,age = ?,idType= ? where id = ?';
    let updateParams = [realname,age,idType,id];
    connection.query(updateSql,updateParams,(err,result)=>{
        if(err){
            console.log('[UPDATE ERROR - ]', err.message);
            result={
                code:'0',
                msg:'服务器内部错误!'
            };
            res.send(result);
        }else{
            result={
                code:'1',
                msg:'操作成功!'
            }
            res.send(result)
        }
    })
});

router.post('/deleteUserById',(req,res)=>{
   let result = {};
   let {id} = req.body;
   let delSql = 'delete from users where id = ?';
   let delParams = [id];
   connection.query(delSql,delParams,(err,result)=>{
       if(err){
           console.log('[DELETE ERROR - ]', err.message);
           result={
               code:'0',
               msg:'服务器内部错误!'
           };
           res.send(result);
       }else{
           result={
               code:'1',
               msg:'操作成功!'
           }
           res.send(result)
       }
   })

});

router.post('/insertUser', (req, res) => {
    let result = {}
    let {username, password, age, realname, idType} = req.body;
    let addSql = 'insert into users(username,password,age,realname,idType) values(?,?,?,?,?)';
    let addSqlParams = [username, password, age, realname, idType];
    connection.query(addSql, addSqlParams, (err, result) => {
        if (err) {
            console.log('[INSERT ERROR - ]', err.message);
            result={
                code:'0',
                msg:'服务器内部错误!'
            }
            res.send(result)

        }else{
            result={
                code:'1',
                msg:'操作成功!'
            }
            res.send(result)
        }
    })

});

// 接收请求
router.post('/checklogin', (req, res) => {
    // 接收用户名和密码
    let {username, password} = req.body;
    console.log(username, password);
    // 去数据库查询是否存在这个账号和密码的用户
    const sqlStr = `select * from users where username = '${username}' and password = '${password}'`;
    connection.query(sqlStr, function (error, data) {
        if (error) {
            throw error
        } else {
            res.send(data);
        }

    })
});

router.post('/userList', (req, res) => {
    const sqlStr = 'select * from users'
    connection.query(sqlStr, (err, data) => {
        if (err) {
            throw err
        } else {
            res.send(data)
        }
    })
});


module.exports = router;


/*
create table users(
    id int primary key auto_increment,
    username varchar(50),
    password varchar(50),
    realname varchar(50),
    idType varchar(50),
    age int
);

insert into users (username,password,realname,idType,age) values('admin','123456','邓武博','37152319920226095X',18);
insert into users (username,password,realname,idType,age) values('guest','123456','小老弟','37152319920226096X',17);

 */